﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using App.WebApi.ViewModels;
using System.Web.Http.Results;
using System.Threading.Tasks;
using App.Authorization.Users;
using App.BaseObject;
using App.Organizations;

namespace App.WebApi.Controllers
{
    public class UserController : AppApiControllerBase
    {
        private readonly ICommonAppService _commonService;
        private readonly IUserAppService _userService;
        private readonly IOrganizationUnitAppService _organizationUnitAppService;
        public UserController(ICommonAppService commonService, IUserAppService userService, IOrganizationUnitAppService organizationUnitAppService)
        {
            _commonService = commonService;
            _userService = userService;
            _organizationUnitAppService = organizationUnitAppService;
        }

        public JsonResult<GridResult> GetUsers(int page, int rows, string sort = "CreationTime", string order = "asc", string filterRules = "", Guid? teamId = null)
        {
            int total;
            var where = "1=1";
            if (teamId != null)
                where += " and c.OrganizationUnit.Id=" + teamId + "";

            var query = _commonService.GetPageRecords<User>(where, page, rows, sort, order, out total,
                filterRules, new[] { "OrganizationUnit", "Roles" });

            return Json(new GridResult { total = total, rows = query });
        }

        [HttpPost]
        public async Task<int[]> GetAllMenuByUserId(long id) {
            var allMenuIds = await _userService.GetAllMenuByUserId(id);
            return allMenuIds.Select(s => s.Id).ToArray();
        }

        public async Task<JsonResult<List<TeamTree>>> GetTeams()
        {
            var data = await _organizationUnitAppService.GeTeams();
            var result = new List<TeamTree>();
            Recursion(new TeamTree { id = null, children = new List<TeamTree>() }, result, data.ToList());
            return Json(result);
        }

        public JsonResult<GridResult> GetLoginLogs(int page, int rows, string sort = "CreationTime", string order = "desc", string filterRules = "")
        {
            int total;
            var where = "c.UserId=" + AbpSession.UserId.Value + "";

            var query = _commonService.GetPageRecords<LoginLog>(where, page, rows, sort, order, out total,
                filterRules);

            return Json(new GridResult { total = total, rows = query });

        }

        #region Helpers
        //递归
        private void Recursion(TeamTree parentNode, IList<TeamTree> result, IList<OrganizationUnit> list)
        {
            foreach (var item in from c in list where c.ParentId == parentNode.id select c)
            {
                var child = new TeamTree { id = item.Id, text = item.TeamName, ParentId = item.ParentId, TeamName = item.TeamName, TeamDesc = item.TeamDesc, expanded = true, children = new List<TeamTree>() };
                if (item.ParentId == null)
                {
                    result.Add(child);
                }
                else
                {
                    child.leaf = true;
                    parentNode.children.Add(child);
                }
                Recursion(child, result, list);
            }
        }
        #endregion
    }
}